package com.zsx.shopping.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsx.shopping.common.api.user.vo.ReceiveAddressVO;
import com.zsx.shopping.common.session.UserVO;
import com.zsx.shopping.user.converter.ReceiveAddressConverter;
import com.zsx.shopping.user.entity.ReceiveAddress;
import com.zsx.shopping.user.form.ReceiveAddressForm;
import com.zsx.shopping.user.interceptor.LoginInterceptor;
import com.zsx.shopping.user.service.ReceiveAddressService;
import com.zsx.shopping.user.mapper.ReceiveAddressMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Service
public class ReceiveAddressServiceImpl extends ServiceImpl<ReceiveAddressMapper, ReceiveAddress>
    implements ReceiveAddressService{

    @Resource
    private ReceiveAddressMapper receiveAddressMapper;

    @Resource
    private ReceiveAddressConverter receiveAddressConverter;

    @Override
    public List<ReceiveAddressVO> getReceiveAddressList() {
        UserVO userVO = LoginInterceptor.loginUser.get();
        List<ReceiveAddress> receiveAddresses = receiveAddressMapper.selectList(new LambdaQueryWrapper<ReceiveAddress>()
                .eq(ReceiveAddress::getUserId, userVO.getId()));
        return receiveAddressConverter.toReceiveAddressVOs(receiveAddresses);
    }

    @Override
    public void addReceiveAddress(ReceiveAddressForm receiveAddressForm) {
        UserVO userVO = LoginInterceptor.loginUser.get();
        ReceiveAddress receiveAddress = receiveAddressMapper.selectOne(new LambdaQueryWrapper<ReceiveAddress>()
                .eq(ReceiveAddress::getUserId, userVO.getId())
                .eq(ReceiveAddress::getDefaultState, true));
        if (receiveAddress != null) {
            receiveAddress.setDefaultState(false);
            receiveAddressMapper.updateById(receiveAddress);
        }
        receiveAddress = receiveAddressConverter.toReceiveAddress(receiveAddressForm, userVO.getId(), true);
        receiveAddressMapper.insert(receiveAddress);
    }

    @Override
    public void updateDefaultReceiveAddress(Long receiveAddressId) {
        UserVO userVO = LoginInterceptor.loginUser.get();
        ReceiveAddress receiveAddress = receiveAddressMapper.selectOne(new LambdaQueryWrapper<ReceiveAddress>()
                .eq(ReceiveAddress::getUserId, userVO.getId())
                .eq(ReceiveAddress::getDefaultState, true));
        if(receiveAddress != null) {
            receiveAddress.setDefaultState(false);
            receiveAddressMapper.updateById(receiveAddress);
        }
        receiveAddress = receiveAddressMapper.selectById(receiveAddressId);
        receiveAddress.setDefaultState(true);
        receiveAddressMapper.updateById(receiveAddress);
    }

    @Override
    public void deleteReceiveAddress(Long receiveAddressId) {
        ReceiveAddress receiveAddress = receiveAddressMapper.selectById(receiveAddressId);
        receiveAddressMapper.deleteById(receiveAddressId);
        // 若已被删除的是默认接收地址,则任意另取一个接收地址设为默认
        if (receiveAddress.getDefaultState()) {
            UserVO userVO = LoginInterceptor.loginUser.get();
            ReceiveAddress one = receiveAddressMapper.selectOne(new LambdaQueryWrapper<ReceiveAddress>()
                    .eq(ReceiveAddress::getUserId, userVO.getId()));
            if (one != null) {
                one.setDefaultState(true);
                receiveAddressMapper.updateById(one);
            }
        }
    }

    @Override
    public ReceiveAddressVO getReceiveAddress(Long receiveAddressId) {
        ReceiveAddress receiveAddress = receiveAddressMapper.selectById(receiveAddressId);
        return receiveAddressConverter.toReceiveAddressVO(receiveAddress);
    }

    @Override
    public ReceiveAddressVO getDefaultReceiveAddress(Long userId) {
        ReceiveAddress receiveAddress = receiveAddressMapper.selectOne(new LambdaQueryWrapper<ReceiveAddress>()
                .eq(ReceiveAddress::getUserId, userId).eq(ReceiveAddress::getDefaultState, true));
        return receiveAddressConverter.toReceiveAddressVO(receiveAddress);
    }
}




